package menus;
import gui.CustomTable;
import gui.CustomTableModel;
import gui.ExceptionListener;
import gui.MainView;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import database.AlbumData;


import java.sql.*;
import java.text.*;
public class AlbumMenu extends MenuSuperclass implements ActionListener, ExceptionListener
{
    private AlbumData albumData = null;
    public AlbumMenu(MainView mv)
    {
        this.mv = mv;
        albumData = new AlbumData();
        albumData.addExceptionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        if (actionCommand.equals("Show Singers"))
        {
            showAllSingers();
            return;
        }
        if (actionCommand.equals("Add Singer"))
        {
            showAllSingers();
        	SingerInsertDialog iDialog = new SingerInsertDialog(mv);
            iDialog.pack();
            mv.centerWindow(iDialog);
            iDialog.setVisible(true);
            return;
        }
        if (actionCommand.equals("Edit Singers"))
        {
            editAllSingers();
            return;
        }
        if (actionCommand.equals("Show Songs"))
        {
            showAllSongs();
            return;
        }
        if (actionCommand.equals("Add Song"))
        {
            showAllSongs();
        	SongInsertDialog iDialog = new SongInsertDialog(mv);
            iDialog.pack();
            mv.centerWindow(iDialog);
            iDialog.setVisible(true);
            return;
        }
        if (actionCommand.equals("Edit Songs"))
        {
            editAllSongs();
            return;
        }
    }
    private void showAllSingers()
    
    {
        ResultSet rs = albumData.showSingers();
        CustomTableModel model = new CustomTableModel(albumData.getConnection(), rs);
        CustomTable data = new CustomTable(model);
        model.addExceptionListener(this);
        data.addExceptionListener(this);
        mv.addTable(data);
    }
    
    private void editAllSingers()
    {
        ResultSet rs = albumData.editSingers();
        CustomTableModel model = new CustomTableModel(albumData.getConnection(), rs);
        CustomTable data = new CustomTable(model);
        model.addExceptionListener(this);
        data.addExceptionListener(this);
        mv.addTable(data);
    }
    
    private void showAllSongs()
    
    {
        ResultSet rs = albumData.showSongs();
        CustomTableModel model = new CustomTableModel(albumData.getConnection(), rs);
        CustomTable data = new CustomTable(model);
        model.addExceptionListener(this);
        data.addExceptionListener(this);
        mv.addTable(data);
    }
    
    private void editAllSongs()
    {
        ResultSet rs = albumData.editSongs();
        CustomTableModel model = new CustomTableModel(albumData.getConnection(), rs);
        CustomTable data = new CustomTable(model);
        model.addExceptionListener(this);
        data.addExceptionListener(this);
        mv.addTable(data);
    }
    
    class SingerInsertDialog extends JDialog implements ActionListener
    {
        private JTextField upcField = new JTextField(10);
        private JTextField sNameField = new JTextField(15);
        public SingerInsertDialog(JFrame parent)
        {
            super(parent, "Add Singer", true);
            setResizable(false);
            JPanel contentPane = new JPanel(new BorderLayout());
            setContentPane(contentPane);
            contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
                                                                  10));
            JPanel inputPane = new JPanel();
            inputPane.setBorder(BorderFactory.createCompoundBorder(
                         new TitledBorder(new EtchedBorder(), "Singer Fields"),
                         new EmptyBorder(5, 5, 5, 5)));
            GridBagLayout gb = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            inputPane.setLayout(gb);
            JLabel label= new JLabel("UPC: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(0, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(0, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(upcField, c);
            inputPane.add(upcField);
            label = new JLabel("Singer Name: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(5, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(5, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(sNameField, c);
            inputPane.add(sNameField);
            sNameField.addActionListener(this);
            sNameField.setActionCommand("OK");
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
            buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 2));
            JButton OKButton = new JButton("OK");
            JButton cancelButton = new JButton("Cancel");
            OKButton.addActionListener(this);
            OKButton.setActionCommand("OK");
            cancelButton.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        dispose();
                    }
                });
            buttonPane.add(Box.createHorizontalGlue());
            buttonPane.add(OKButton);
            buttonPane.add(Box.createRigidArea(new Dimension(10,0)));
            buttonPane.add(cancelButton);
            contentPane.add(inputPane, BorderLayout.CENTER);
            contentPane.add(buttonPane, BorderLayout.SOUTH);
            addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        dispose();
                    }
                });
        }
        public void actionPerformed(ActionEvent e)
        {
            String actionCommand = e.getActionCommand();
            if (actionCommand.equals("OK"))
            {
                if (validateInsert() != VALIDATIONERROR)
                {
                    dispose();
                }
                else
                {
                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane errorPopup = new JOptionPane();
                    errorPopup.showMessageDialog(this, "Invalid Input",
                                    "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        private int validateInsert()
        {
            try
            {
                Integer          supc;
                String         sname;
                
                if (upcField.getText().trim().length() != 0 && sNameField.getText().trim().length() != 0)
                {
                    supc = Integer.valueOf(upcField.getText().trim());
                    sname = sNameField.getText().trim();
                    
                    if (albumData.findSingerAndUPC(supc.intValue(), sname))
                    {
                        Toolkit.getDefaultToolkit().beep();
                        mv.updateStatusBar("That tuple already exists!");
                        return OPERATIONFAILED;
                    }
                }
                else
                {
                    return VALIDATIONERROR;
                }
                if (sNameField.getText().trim().length() != 0)
                {
                    sname = sNameField.getText().trim();
                }
                else
                {
                    return VALIDATIONERROR;
                }
                                mv.updateStatusBar("Inserting singer...");
                if (albumData.insertSinger(supc, sname))
                {
                    mv.updateStatusBar("Operation successful.");
                    showAllSingers();
                    return OPERATIONSUCCESS;
                }
                else
                {
                    Toolkit.getDefaultToolkit().beep();
                    mv.updateStatusBar("Operation failed.");
                    return OPERATIONFAILED;
                }
            }
            catch (NumberFormatException ex)
            {
                return VALIDATIONERROR;
            }
        }
    }
    class SongInsertDialog extends JDialog implements ActionListener
    {
        private JTextField upcField = new JTextField(10);
        private JTextField sTitleField = new JTextField(15);
        public SongInsertDialog(JFrame parent)
        {
            super(parent, "Add Song", true);
            setResizable(false);
            JPanel contentPane = new JPanel(new BorderLayout());
            setContentPane(contentPane);
            contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
                                                                  10));
            JPanel inputPane = new JPanel();
            inputPane.setBorder(BorderFactory.createCompoundBorder(
                         new TitledBorder(new EtchedBorder(), "Song Fields"),
                         new EmptyBorder(5, 5, 5, 5)));
            GridBagLayout gb = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            inputPane.setLayout(gb);
            JLabel label= new JLabel("UPC: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(0, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(0, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(upcField, c);
            inputPane.add(upcField);
            label = new JLabel("Song Name: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(5, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(5, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(sTitleField, c);
            inputPane.add(sTitleField);
            sTitleField.addActionListener(this);
            sTitleField.setActionCommand("OK");
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
            buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 2));
            JButton OKButton = new JButton("OK");
            JButton cancelButton = new JButton("Cancel");
            OKButton.addActionListener(this);
            OKButton.setActionCommand("OK");
            cancelButton.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        dispose();
                    }
                });
            buttonPane.add(Box.createHorizontalGlue());
            buttonPane.add(OKButton);
            buttonPane.add(Box.createRigidArea(new Dimension(10,0)));
            buttonPane.add(cancelButton);
            contentPane.add(inputPane, BorderLayout.CENTER);
            contentPane.add(buttonPane, BorderLayout.SOUTH);
            addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        dispose();
                    }
                });
        }
        public void actionPerformed(ActionEvent e)
        {
            String actionCommand = e.getActionCommand();
            if (actionCommand.equals("OK"))
            {
                if (validateInsert() != VALIDATIONERROR)
                {
                    dispose();
                }
                else
                {
                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane errorPopup = new JOptionPane();
                    errorPopup.showMessageDialog(this, "Invalid Input",
                                    "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        private int validateInsert()
        {
            try
            {
                Integer          supc;
                String         stitle;
                
                if (upcField.getText().trim().length() != 0 && sTitleField.getText().trim().length() != 0)
                {
                    supc = Integer.valueOf(upcField.getText().trim());
                    stitle = sTitleField.getText().trim();
                    
                    if (albumData.findSongAndUPC(supc.intValue(), stitle))
                    {
                        Toolkit.getDefaultToolkit().beep();
                        mv.updateStatusBar("That tuple already exists!");
                        return OPERATIONFAILED;
                    }
                }
                else
                {
                    return VALIDATIONERROR;
                }
                if (sTitleField.getText().trim().length() != 0)
                {
                    stitle = sTitleField.getText().trim();
                }
                else
                {
                    return VALIDATIONERROR;
                }
                                mv.updateStatusBar("Inserting song...");
                if (albumData.insertSong(supc, stitle))
                {
                    mv.updateStatusBar("Operation successful.");
                    showAllSongs();
                    return OPERATIONSUCCESS;
                }
                else
                {
                    Toolkit.getDefaultToolkit().beep();
                    mv.updateStatusBar("Operation failed.");
                    return OPERATIONFAILED;
                }
            }
            catch (NumberFormatException ex)
            {
                return VALIDATIONERROR;
            }
        }
    }

}
